<?php

namespace App\Http\Controllers\V1\Admin;

use App\Models\V1\Navigation;
use Illuminate\Http\Request;

class NavigationController extends BaseController
{
    /**
     * 数据列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $query=Navigation::query();
        if($request->filled('title')){
            $query->where('title','like',"%".$request->input('title')."%");
        }
        $list=$query->get();
        return ajax_output(200,"成功",$list);
    }


    /**
     * 保存数据
     * @param Request $request
     * @return void
     */
    public function store(Request $request)
    {
        $model=new Navigation();
        $model->create([
            'title'=>$request->input('title',''),
            'url'=>$request->input('url',''),
            'pid'=>$request->input('pid',0),
            'is_outside'=>$request->input('is_outside',0),
            'target'=>$request->input('target','')
        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 数据详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id)
    {
        $model=new Navigation();
        $vo=$model->find($id);
        return ajax_output(200,"成功",$vo);
    }

    /**
     * 更新数据
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, $id)
    {
        $model=new Navigation();
        $vo=$model->find($id);
        if(!$vo){
            return ajax_output(300,"数据不存在");
        }
        $vo->update([
            'title'=>$request->input('title',''),
            'url'=>$request->input('url',''),
            'pid'=>$request->input('pid',0),
            'is_outside'=>$request->input('is_outside',0),
            'target'=>$request->input('target','')
        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 删除数据
     * @param $id
     * @return void
     */
    public function destroy($id)
    {
        Navigation::destroy($id);
        return ajax_output(200,"成功");
    }
}
